package com.easy.controller;

import com.easy.util.Result;
import com.easy.util.SysConfig;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("file")
@CrossOrigin
public class FileUploadController {
    @RequestMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {

        //将文件上传到指定职位
        //文件上传时的名字
        String oldName=file.getOriginalFilename();
        String lastName=oldName.substring(oldName.lastIndexOf(".")); //.jpeg   .txt   .png

        String newName= UUID.randomUUID().toString()+lastName; //XXXXXXXXXX.jpeg
        File target=new File(SysConfig.UPLOAD_DIR+"/"+newName);
        file.transferTo(target);
        //返回该文件访问路径
        String path=SysConfig.UPLOAD_PATH+newName;
        return Result.success("上传成功",path);
    }
}
